//
//  MYXPayPasswordViewController.m
//  meiyinxiang
//
//  Created by 媒印象 on 22/11/2017.
//  Copyright © 2017 hbjApple. All rights reserved.
//

#import "MYXPayPasswordViewController.h"
#import "CYPasswordView.h"
#import <MBProgressHUD/MBProgressHUD.h>
#define kRequestTime 3.0f
#define kDelay 1.0f
@interface MYXPayPasswordViewController ()
@property (nonatomic, strong) CYPasswordView *passwordView;

@end

@implementation MYXPayPasswordViewController
static BOOL flag = NO;

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - PrivateMethod

- (void)createUI{
    /** 注册取消按钮点击的通知 */
    [CYNotificationCenter addObserver:self selector:@selector(cancel) name:CYPasswordViewCancleButtonClickNotification object:nil];
    [CYNotificationCenter addObserver:self selector:@selector(forgetPWD) name:CYPasswordViewForgetPWDButtonClickNotification object:nil];
    __weak typeof(self) weakself  = self;
    self.passwordView = [[CYPasswordView alloc] init];
    self.passwordView.title = @"输入交易密码";
    self.passwordView.loadingText = @"提交中...";
    [self.passwordView showInView:self.view];
    
    self.passwordView.finish = ^(NSString *password) {
        [weakself.passwordView hideKeyboard];
        [weakself.passwordView startLoading];
        CYLog(@"cy ========= 发送网络请求  pwd=%@", password);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kRequestTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            flag = !flag;
            if (flag) {
                CYLog(@"申购成功，跳转到成功页");
                [weakself.passwordView requestComplete:YES message:@"申购成功，做一些处理"];
                [weakself.passwordView stopLoading];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakself.passwordView hide];
                });
            } else {
                CYLog(@"申购失败，跳转到失败页");
            }
            
        });
    };
}
- (void)cancel {
    CYLog(@"关闭密码框");
}

- (void)forgetPWD {
    CYLog(@"忘记密码");
}
#pragma mark -TXTTradedelegate



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
